package com.yingxin.yxerp.module.organization.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.taobao.api.ApiException;
import com.yingxin.yx.framework.commons.helper.ListHelper;
import com.yingxin.yxerp.module.organization.entity.Department;
import com.yingxin.yxerp.module.organization.mapper.DepartmentMapper;
import com.yingxin.yxerp.module.organization.mapper.DepartmentMgrMapper;
import com.yingxin.yxerp.module.organization.service.IDepartmentService;
import com.yingxin.yxerp.module.organization.service.helper.DepartmentTreeHelper;
import com.yingxin.yxerp.module.organization.vo.req.DepartmentCreateVo;
import com.yingxin.yxerp.module.organization.vo.req.DepartmentMgrQueryVo;
import com.yingxin.yxerp.module.organization.vo.req.DepartmentQueryVo;
import com.yingxin.yxerp.module.organization.vo.req.DepartmentUpdateVo;
import com.yingxin.yxerp.module.organization.vo.resp.DepartmentMgrRespVo;
import com.yingxin.yxerp.module.organization.vo.resp.DepartmentRespVo;
import com.yingxin.yxerp.module.sys.entity.Company;
import com.yingxin.yxerp.module.sys.service.ICompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 部门 前端控制器
 * @author fanchao
 * @since 2021-05-13
 */

@ApiSort(20)
@RestController
@RequestMapping("/organization/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    @SuppressWarnings("all")
    private DepartmentMapper departmentMapper;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    @SuppressWarnings("all")
    private DepartmentMgrMapper departmentMgrMapper;

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list2")
    public List<DepartmentRespVo> list2(@RequestBody DepartmentQueryVo queryVo) {
        List<DepartmentRespVo> deptList = departmentMapper.listVo(queryVo);
        Map<String, DepartmentRespVo> deptMap = ListHelper.toMap(DepartmentRespVo::getId, deptList);
        // 部门负责人
        List<DepartmentMgrRespVo> deptMgrList = departmentMgrMapper.listVo(new DepartmentMgrQueryVo());
        for (DepartmentMgrRespVo mgr : deptMgrList) {
            DepartmentRespVo dept = deptMap.get(mgr.getDeptId());
            if (dept != null) {
                dept.getManagerList().add(mgr);
            }
        }
        return DepartmentTreeHelper.build(deptList);
    }

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<DepartmentRespVo> list(@RequestBody DepartmentQueryVo queryVo) {
        List<DepartmentRespVo> departments = departmentMapper.listVo(queryVo);
        return DepartmentTreeHelper.build(departments);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public DepartmentRespVo get(@PathVariable("id") String id) {
        return departmentMapper.getVo(id);
    }

    @PreAuthorize("hasAuthority('sys:dept:create')")
    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid DepartmentCreateVo createVo) {
        Department department = new Department();
        BeanUtil.copyProperties(createVo, department);
        departmentService.create(department, createVo.getManagerIds());
        return department.getId();
    }

    @PreAuthorize("hasAuthority('sys:dept:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid DepartmentUpdateVo updateVo) {
        Department department = new Department();
        BeanUtil.copyProperties(updateVo, department);
        departmentService.update(department, updateVo.getManagerIds());
        return department.getId();
    }

    @PreAuthorize("hasAuthority('sys:dept:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        Department dept = departmentService.getById(id);
        departmentService.delete(dept);
    }

    @ApiOperation(value = "分公司列表")
    @PostMapping("/listCompany")
    public List<Company> listCompany() {
        return companyService.list();
    }

    @PreAuthorize("hasAuthority('sys:dept:synchronism')")
    @ApiOperation(value = "部门同步")
    @PostMapping("/synchronism")
    public void synchronism() throws ApiException {
        departmentService.synchronismWeCom();
    }

}
